<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib uri="/authtaglib" prefix="auth" %>
<!DOCTYPE html>
<html>
<head>
<title>人员写卡</title>
<jsp:include page="/include.jsp"/>
<script type="text/javascript">
//新增数据源配置信息窗口
function writeCard() {
    var row = $('#datagrid').datagrid('getSelected')
    if (row == null) {
        $.messager.alert('温馨提示', '请选中一个需要写卡的人员');
        return;
    }

    $('<div id="writeCardDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!writing.action?id=' + row.id,
        top: 10,
        height:300,
        width: 500,
        modal: true,
        title: '写卡',
        onClose: function () {
            $(this).dialog('destroy');
        }
    });
}
function bindPage() {
    $('<div id="addDiv"/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/employeeAction!bindPage.action',
        top: 10,
        width: 700,
        modal: true,
        title: '设备绑定',
        buttons: [
            {
                id: 'btnSave',
                text: '保存',
                iconCls: '',
                handler: function () {
                    var d = $(this).closest('.window-body');
                    $('#add_form').form('submit', {
                        url: '${pageContext.request.contextPath}/cnbdw/employeeAction!add.action',
                        onSubmit: function () {
                            if ($(this).form("validate")) {
                                $.messager.progress({title: '提示', text: '正在保存，请稍候 ....'});
                            } else {
                                return false;
                            }
                        },
                        success: function (result) {
                            try {
                                var r = $.parseJSON(result);
                                if (r.returnFlag) {
                                    $('#datagrid').datagrid('reload');
                                    d.dialog('destroy');
                                }
                                //显示提示信息
                                $.messager.show({
                                    title: '提示',
                                    msg: r.returnMsg
                                });
                            } catch (e) {
                            }
                            $.messager.progress('close');
                        }
                    });
                }
            },
            {
                id: 'btnCancle',
                text: '取消',
                iconCls: '',
                handler: function () {
                    $(this).closest('.window-body').dialog('destroy');
                }
            }
        ],
        onClose: function () {
            $(this).dialog('destroy');
            //刷新主列表
            $('#datagrid').datagrid('reload');
        }
    });
}
//执行查询
function doQuery() {
    if ($("#datasource_query_form").form("validate")) {
        $("#datagrid").datagrid('options').queryParams = serializeObject($('#datasource_query_form')); //把查询条件赋值给datagrid内部变量
        $("#datagrid").datagrid('load'); //重新加载
    }
}
//重置查询条件
function doReset() {
    $('#datasource_query_form').form('reset');
}
//页面加载完后响应 onReady
$(function () {
    //查询按纽绑定事件
    $('#btnQuery').bind('click', doQuery);
    //重置按纽绑定事件
    $('#btnReset').bind('click', doReset);
    //初始化主列表
    $('#datagrid').datagrid({
        url: '${pageContext.request.contextPath}/cnbdw/employeeAction!stayDatasource.action',
        fit: true,
        singleSelect: true,
        border: false,
        fitColumns: true,
        pagination: true,
        pageList: [10, 20, 30],
        rownumbers: false,
        sortName: 'id',
        sortOrder: 'desc',
        striped: true,
        columns: [
            [
                //{field: 'id', title: '唯一编号', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                //{field: 'namePy', title: 'namePy', align: 'left', formatter: function (value, rowData, rowIndex) {if (value == '') {return '--'} return value;}},
                {field: 'photo', title: '照片', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if ($.trim(value) != '') {
                        return '<img src="${pageContext.request.contextPath}' + value + '" style="width: 30px; height: 30px;">';
                    }
                    return '--';
                }},
                {field: 'name', title: '姓名', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (value == '') {
                        return '--'
                    }
                    return value;
                }},
                {field: 'cardNo', title: '证件号码', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (value == '') {
                        return '--'
                    }
                    return value;
                }},
                {field: 'gender', title: '入住信息', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (rowData.bedInfo == null) {
                        return '未入住'
                    } else {
                        var _htmls = [];
                        _htmls.push(getRoomNo(rowData.bedInfo, $('#datagrid').datagrid('getData')));
                        _htmls.push(getBadNum(rowData.bedInfo));
                        return _htmls.join("");
                    }
                    return value;
                }},
                {field: 'duties', title: '绑定设备', align: 'left', formatter: function (value, rowData, rowIndex) {
                    if (rowData.employeeDevices == null || rowData.employeeDevices.length == 0) {
                        return '未绑定任何设备'
                    } else {
                        var _htmls = [];
                        $.each(rowData.employeeDevices, function (index, edevice) {
                            if (edevice.device) {
                                var deviceType = edevice.device.deviceType;
                                if (index != 0) {
                                    _htmls.push(", ");
                                }

                                if (deviceType == 1) {
                                    _htmls.push("智能卡");
                                } else {
                                    _htmls.push("手表");
                                }

                                _htmls.push("(编号:");
                                _htmls.push(edevice.device.deviceNo);
                                _htmls.push(")");
                            }
                        });
                        return _htmls.join("")
                    }
                    return value;
                    return value;
                }}
            ]
        ],
        toolbar: '#datagrid_toobar'
    });
});

function getRoomNo(bed, data) {
    var room = bed.room;
    if (bed.room && bed.room.$ref) {
        room = jsonPath(data, bed.room.$ref);
        return "[" + room[0].roomNo + "]号房";
    }
    if (room != null) {
        return "[" + room.roomNo + "]号房";
    }
    return "--";
}
function getBadNum(bed) {
    if (bed == null) {
        return "--";
    }

    return badNum = "[" + bed.bedNo + "]床";
}


function showFileUpload(callbackId) {
    window.top.$('<div/>').dialog({
        href: '${pageContext.request.contextPath}/cnbdw/commons/file_upload.jsp',
        modal: true,
        top: ($(window.top).height() - 400) * 0.5,
        left: ($(window.top).width() - 600) * 0.5,
        width: 400,
        title: '上传附件',
        buttons: [
            {
                text: '保存',
                iconCls: '',
                id: 'fileUploadDialog',
                handler: function () {
                    //获取专题性别参数
                    var d = window.top.$(this).closest('.window-body');
                    if (window.top.fileValidateData()) {
                        window.top.$('#fileUploadForm').form('submit', {
                            url: '${pageContext.request.contextPath}/fileUploadAction!uploadFile.action',
                            success: function (result) {
                                try {
                                    var r = $.parseJSON(result);
                                    if (r.err != null && r.err != '') {
                                        //显示提示信息
                                        $.messager.show({
                                            title: '提示',
                                            msg: r.err
                                        });
                                    } else {
                                        $('#' + callbackId).val(r.msg);
                                        $('#photoPanel').attr('src', '${pageContext.request.contextPath}' + r.msg);
                                        //关闭新增窗口
                                        d.dialog('destroy');
                                    }
                                } catch (e) {
                                    $.messager.alert('提示', result);
                                }
                            }
                        });
                    }
                }
            }
        ],
        onClose: function () {
            window.top.$(this).dialog('destroy');
        }
    });
}
function showBindHistory(callbackId) {
    window.top.$('<iframe width="100%" height="' + ($(window.top).height() - 340) + '" src="${pageContext.request.contextPath}/cnbdw/employeeDeviceHistoryAction!indexPage.action"/>').dialog({
        modal: true,
        top: 0,
        left: ($(window.top).width() - 600) * 0.5,
        width: 800,
        height: $(window.top).height() - 300,
        title: '设备绑定历史',
        onClose: function () {
            window.top.$(this).dialog('destroy');
        }
    });
}
function showStayHistory(callbackId) {
    window.top.$('<iframe width="100%" height="' + ($(window.top).height() - 340) + '" src="${pageContext.request.contextPath}/cnbdw/employeeRoomHistoryAction!indexPage.action"/>').dialog({
        modal: true,
        top: 0,
        left: ($(window.top).width() - 600) * 0.5,
        width: 800,
        height: $(window.top).height() - 300,
        title: '入住历史',
        onClose: function () {
            window.top.$(this).dialog('destroy');
        }
    });
}
</script>
</head>
<body class="easyui-layout">
<form id="datasource_query_form" method="post">
    <div data-options="region:'north'" style="height:60px;border-bottom:none;border-top:none;">
        <table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
            <tr>
                <td style="width:1%;text-align:left;" nowrap="nowrap">
                    <label for="datasource_datsourcename">人员名称：</label>
                </td>
                <td style="width:15%;text-align:left;" nowrap="nowrap">
                    <input type="text" id="datasource_datsourcename" name="name" class="easyui-validatebox"
                           data-options="validType:'specialCharFilter'"/>
                </td>
                <td colspan="2" style="width:25%;text-align:right;" nowrap="nowrap">
                    <input type="button" id="btnQuery" value="查 询" class="search_btn"/>
                    <input type="button" id="btnReset" value="重 置" class="reset_btn"/>
                </td>
            </tr>
        </table>
    </div>
    <div data-options="region:'center'" style="border-top:none">
        <table id="datagrid"></table>
        <div id="datagrid_toobar" class="etl_panel_title">
            <auth:button authUrl="cnbdw/employeeAction!add.action">
                <a href="javascript:void(0);" onclick="writeCard()" class="easyui-linkbutton"
                   data-options="iconCls:'icon-tip',plain:true" style="float:left;">写卡</a>
            </auth:button>
          <%--   <div class="datagrid-btn-separator"></div>
            <auth:button authUrl="cnbdw/employeeAction!add.action">
                <a href="javascript:void(0);" onclick="bindPage()" class="easyui-linkbutton"
                   data-options="iconCls:'icon-ok',plain:true" style="float:left;">写入智能设备</a>
            </auth:button>
            --%>
        </div>
    </div>
</form>
</body>
</html>

